#include "playbackwidget.h"

#include <QHBoxLayout>
#include <QVBoxLayout>

#include <QPushButton>
#include <QUrl>

#include "activelogs.h"
#ifdef PLAYBACK_WIDGET
    #define ENABLE_FILE_LOG
#endif
#include "logmacros.h"

#define VIDEO_LOCAL "c:/Data/example.mp4"
#define VIDEO_REMOTE "rtsp://v8.cache1.c.youtube.com/CiILENy73wIaGQnNUH6lxLd0MRMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"


PlaybackWidget::PlaybackWidget(QWidget *parent)
    : QWidget(parent)
{
	QDEBUG_ENTER_FN();
	m_media = new MediaObject(parent);
	connect(m_media, SIGNAL(stateChanged( Phonon::State, Phonon::State)), this, SLOT(stateChanged( Phonon::State, Phonon::State)));
	m_videoWidget = new VideoWidget(parent);
	m_videoWidget->winId(); // hack needed on s60 5th and S^3
	m_videoWidget->showMaximized();
    Phonon::createPath(m_media, m_videoWidget);
	
	QHBoxLayout* layout = new QHBoxLayout(this); 
	layout->addWidget(m_videoWidget, 7);
	
	
	m_playButton = new QPushButton("play", this);
	m_pauseButton = new QPushButton("pause", this);
	m_stopButton = new QPushButton("stop", this);
	m_hideButton = new QPushButton("hide", this);
	
	connect(m_playButton, SIGNAL(clicked(bool)), m_media, SLOT(play()));
	connect(m_pauseButton, SIGNAL(clicked(bool)), m_media, SLOT(pause()));
	connect(m_stopButton, SIGNAL(clicked(bool)), m_media, SLOT(stop()));
	connect(m_hideButton, SIGNAL(clicked(bool)), m_stopButton, SIGNAL(clicked(bool)));
	
	QVBoxLayout* vlayout = new QVBoxLayout(this);
	vlayout->addStretch(1);
	vlayout->addWidget(m_playButton, 1);
	vlayout->addStretch(1);
	vlayout->addWidget(m_pauseButton, 1);
	vlayout->addStretch(1);
	vlayout->addWidget(m_stopButton, 1);
	vlayout->addStretch(1);
	vlayout->addWidget(m_hideButton, 1);
	vlayout->addStretch(1);
	
	layout->addLayout(vlayout, 3);
	
	setLayout(layout);
	QDEBUG_EXIT_FN();
}

PlaybackWidget::~PlaybackWidget()
{

}

void PlaybackWidget::stateChanged ( Phonon::State newstate, Phonon::State oldstate ) {
	QDEBUG_ENTER_FN();
	QDEBUG("passing from" << stateToString(oldstate) << "to" << stateToString(newstate));
	if(newstate == Phonon::ErrorState) {
		QDEBUG("error was:" << m_media->errorString());
	}
	QDEBUG_EXIT_FN();
}

QString PlaybackWidget::stateToString(Phonon::State aState) { 
	switch(aState) {
		case Phonon::LoadingState:
			return QString("Phonon::LoadingState");
		case Phonon::StoppedState:
			return QString("Phonon::StoppedState");
		case Phonon::PlayingState:
			return QString("Phonon::PlayingState");
		case Phonon::BufferingState:
			return QString("Phonon::BufferingState");
		case Phonon::PausedState:
			return QString("Phonon::PausedState");
		case Phonon::ErrorState:
			return QString("Phonon::ErrorState");
	}
	return QString();
}

void PlaybackWidget::setLocalUrl() {
	QDEBUG_ENTER_FN();
	QUrl url = QUrl::fromLocalFile(VIDEO_LOCAL);
	setMediaUrl(url);
	QDEBUG_EXIT_FN();
}

void PlaybackWidget::setRemoteUrl() {
	QDEBUG_ENTER_FN();
	QUrl url(VIDEO_REMOTE);
	setMediaUrl(url);
	QDEBUG_EXIT_FN();
}


void PlaybackWidget::setMediaUrl(QUrl aUrl) {
	QDEBUG_ENTER_FN();
	QDEBUG("link to video" << aUrl);
	m_media->setCurrentSource( aUrl );
	QDEBUG_EXIT_FN();
}


